﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        Login1.FindControl("UserName").Focus();        
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) {
        string userName = Login1.UserName;
        string pwd = Login1.Password;
        if (userName != "Admin"){
            Login1.FailureText = "Đăng nhập thất bại!";
            return;
        }
        SimDSTableAdapters.tUserTableAdapter uAdapter = new SimDSTableAdapters.tUserTableAdapter();
        SimDS.tUserDataTable uTable = uAdapter.GetData();
        if (uTable.Count != 1) {
            Login1.FailureText = "Đăng nhập thất bại!";
            return;
        }
        string db_salt = uTable[0].UID;
        string db_hash = uTable[0].Hash;
        bool kq = GlobalFunctions.verifyPassword(pwd, db_hash, db_salt);
        if (kq == true) {
            GlobalFunctions gf = new GlobalFunctions();
            gf.initSession("Admin");
            e.Authenticated = true;
            Response.Redirect("~/admin/admin_index.aspx");
        }
        else {
            Login1.FailureText = "Sai mật khẩu!";
            return;
        }


    }
}
